import requests
from django.test import TestCase

from drf.models import Demo
from drf.serializers import Demo1Serializer, DemoSerializer


# Create your tests here.
class MyTest(TestCase):
    def test_t1(self):
        print('this is test')
        res = requests.get('http://127.0.0.1:8000/groups/?format=json')
        print(res.text)
        self.assertEqual(res.status_code, 200)

    # python manage.py test drf.tests.MyTest.test_t2
    def test_t2(self):
        data={'name':'xxxaaa', 'age':1}
        ## 通过实例保存
        demo=Demo(**data)
        demo.save()
        ## 通过Serialize 保存
        serilize = DemoSerializer(data=data)
        if serilize.is_valid():
            serilize.save()
        else:
            print(serilize.validated_data)
            error=list(serilize.errors.values())
            print('error:',error[0][0])    

        ## 通过ModelSerializer保存
        serilize1 = Demo1Serializer(data=data)
        # print(repr(serilize1))
        if serilize1.is_valid():
            serilize1.save()
        else:
            error=list(serilize1.errors.values())
            print('error:',error[0][0])         
        # print("data:{0},validate:{1}".format(serilize.data,serilize.validate()))
